//
//  OCLoginViewController.m
//  League
//
//  Created by JackChen on 2018/2/15.
//  Copyright © 2018年 ciyuanjuzhen. All rights reserved.
//

#import "OCLoginViewController.h"
#import "OCRegisterViewController.h"
#import "LoginModel.h"

@interface OCLoginViewController ()

@property (assign, nonatomic) NSInteger selectNumIndex;
@property (strong, nonatomic) NSArray *counterNum;
@property (weak, nonatomic) IBOutlet UIButton *areaCodeButton;
@property (weak, nonatomic) IBOutlet UIButton *languageButton;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UIButton *registerButton;
@property (weak, nonatomic) IBOutlet UIButton *rememberButton;
@property (weak, nonatomic) IBOutlet UIButton *forgetButton;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

@end

@implementation OCLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.selectNumIndex = 0;
    self.counterNum = [NSArray arrayWithObjects:@"+86",@"+852", nil];
    if ([self.systemLanguage isEqualToString:@"en"]) {
        //判断当前的语言，进行改变
        [self.languageButton setTitle:@"中文" forState:UIControlStateNormal];
    } else {
        [self.languageButton setTitle:@"English" forState:UIControlStateNormal];
    }
    [self changeLanguage];
    self.view.backgroundColor = sRGBColor(68, 82, 96);
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *firstName = [defaults objectForKey:kIsremberPw];
    if([firstName isEqualToString:kIsremberStr]) {
        self.rememberButton.selected = YES;
        self.phoneTextField.text = [defaults objectForKey:kUserName];
        self.passwordTextField.text = [defaults objectForKey:kUserPassWord];
    } else {
        self.rememberButton.selected = NO;
    }
    //是否自动登录
    if([[defaults objectForKey:kIsLogin] isEqualToString:kLoginStr]){
        if(![defaults objectForKey:kUserName] || ![defaults objectForKey:kUserPassWord]){
            return;
        }
        self.phoneTextField.text = [defaults objectForKey:kUserName];
        self.passwordTextField.text = [defaults objectForKey:kUserPassWord];
        self.rememberButton.selected = YES;
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)changeLanguage {
    NSBundle *bundle = self.languageBundle;
    [self.loginButton setTitle:[bundle localizedStringForKey:@"log" value:nil table:@"hello"] forState:UIControlStateNormal];
    [self.registerButton setTitle:[bundle localizedStringForKey:@"regis" value:nil table:@"hello"] forState:UIControlStateNormal];
    [self.forgetButton setTitle:[bundle localizedStringForKey:@"forgetPW" value:nil table:@"hello"] forState:UIControlStateNormal];
    [self.forgetButton setTitle:[bundle localizedStringForKey:@"forgetPW" value:nil table:@"hello"] forState:UIControlStateNormal];
    self.phoneTextField.placeholder = [bundle localizedStringForKey:@"PhonePlaceholder" value:nil table:@"hello"];
    self.passwordTextField.placeholder = [bundle localizedStringForKey:@"PwPlaceholder" value:nil table:@"hello"];
    [self.rememberButton setTitle:[bundle localizedStringForKey:@"checkbox" value:nil table:@"hello"] forState:UIControlStateNormal];
}

- (IBAction)areaCodeAction:(UIButton *)sender {
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {
        
    }];
    UIAlertAction *walkingBtn = [UIAlertAction actionWithTitle:@"+86" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        self.selectNumIndex = 0;
        [self.areaCodeButton setTitle:self.counterNum[self.selectNumIndex] forState:UIControlStateNormal];
    }];
    UIAlertAction *gaodeNavigation = [UIAlertAction actionWithTitle:@"+852" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.selectNumIndex = 1;
        [self.areaCodeButton setTitle:self.counterNum[self.selectNumIndex] forState:UIControlStateNormal];
    }];
    [alertVc addAction:cancle];
    [alertVc addAction:walkingBtn];
    [alertVc addAction:gaodeNavigation];
    [self presentViewController:alertVc animated:YES completion:nil];
}

- (IBAction)changeLaguageButton:(UIButton *)sender {
    NSString *lan = [InternationalControl userLanguage];
    if([lan isEqualToString:@"en"]){//判断当前的语言，进行改变
        [InternationalControl setUserlanguage:@"zh-Hans"];
        [self.languageButton setTitle:@"English" forState:UIControlStateNormal];
    } else {
        [InternationalControl setUserlanguage:@"en"];
        [self.languageButton setTitle:@"中文" forState:UIControlStateNormal];
    }
    [self changeLanguage];
}

- (IBAction)loginAction:(UIButton *)sender {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary *dic = @{
                          @"phone":self.phoneTextField.text,
                          @"password":self.passwordTextField.text
                          };
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",kFirstUrl,kLoginUrl];
    NSLog(@"urlStr:%@",urlStr);
    [DataContrller PostRequestUrl:urlStr Param:dic requestSuccess:^(NSDictionary *backDic) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"backDic:%@",backDic);
        if ([backDic[@"returnCode"] integerValue] == 200) {
            LoginModel *model = [[LoginModel alloc] initWithDictionary:backDic[@"obj"] error:nil];
            UserInfomation *user = [UserInfomation shareManager];
            user.username = model.username;
            user.phone = model.phone;
            user.pk = model.pk;
            user.rongYunToken = model.rongYunToken;
            user.picUrl = model.picUrl;
            //跳转首页
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:kLoginStr forKey:kIsLogin];
            NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
            [center postNotificationName:kChangeLoginNotificationName object:nil userInfo:nil];
            //记住账号密码
            NSString *firstName = [defaults objectForKey:kIsremberPw];
            if([firstName isEqualToString:kIsremberStr])
            {
                [defaults setObject:self.phoneTextField.text forKey:kUserName];
                [defaults setObject:self.passwordTextField.text forKey:kUserPassWord];
            }
        } else {
            [self showErrorsWithMessage:backDic];
        }
    } requestFail:^(NSString *backErrorStr) {
        NSLog(@"backErrorStr:%@",backErrorStr);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
    
}

- (IBAction)registerAction:(UIButton *)sender {
    OCRegisterViewController *registerVC = [self  initializeStoryboardViewControllerWithStoryboardID:@"LoginRegister" viewcontroller:@"OCRegisterViewController"];
    registerVC.state = OCRegister;
    [self.navigationController pushViewController:registerVC animated:YES];
}

- (IBAction)remenberAction:(UIButton *)sender {
    if(sender.selected == NO){
        sender.selected = YES;
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:kIsremberStr forKey:kIsremberPw];
        if (![self.phoneTextField.text isEqualToString:@""]) {
            [defaults setObject:self.phoneTextField.text forKey:kUserName];
        }
        if (![self.passwordTextField.text isEqualToString:@""]) {
            [defaults setObject:self.passwordTextField.text forKey:kUserPassWord];
        }
        [defaults synchronize];
    } else {
        sender.selected = NO;
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:kNoremberStr forKey:kIsremberPw];
        [defaults removeObjectForKey:kUserName];
        [defaults removeObjectForKey:kUserPassWord];
        [defaults synchronize];
    }
}

- (IBAction)forgetAction:(UIButton *)sender {
    OCRegisterViewController *registerVC = [self  initializeStoryboardViewControllerWithStoryboardID:@"LoginRegister" viewcontroller:@"OCRegisterViewController"];
    registerVC.state = OCForget;
    [self.navigationController pushViewController:registerVC animated:YES];
}

@end
